// pages/minenews/minenews.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    actIdflag: false,
    salIdflag: false,
    infIdflag: false,
    partnerflag: false,
    newslist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //get_message_center
    if (config.sh_is_partner) {
      this.setData({
        partnerflag: true
      })
    }
    this.setData({
      actId: options.actId,
      salId: options.salId,
      infId: options.infId,
    })
    var that = this
    shihan.sendRequest(config.get_message_center, 'POST', {})
      .then(function(response) {
        console.log(response)
        if (response.data.code == 0) {
          that.setData({
            newslist: response.data.data
          })
          wx.setStorage({
            key: 'lastIds',
            data: {
              sh_activity_lastid: '',
              sh_customer_salesrule_lastid: '',
              sh_partner_inform_lastid: ''
            }
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  opennewsdetail: function(e) {
    var index = e.currentTarget.dataset.index; //2-世瀚活动；3-合伙人消息；4-优惠券消息
    if (index > 1) {
      if (index == 3) {
        wx.navigateTo({
          url: '/pages/partner/mynews/mynews'
        })
      } else {
        var title = e.currentTarget.dataset.tit;
        wx.navigateTo({
          url: '/pages/other/minenewslist/minenewslist?tit=' + title + '&index=' + index
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var lastIds = wx.getStorageSync('lastIds') || '';
    var messageids = wx.getStorageSync('messageids') || '';
    //{"sh_activity_id":230,"sh_partner_inform_id":1,"sh_customer_salesrule_id":0}
    if (messageids && Object.keys(messageids).length > 0) {
      if (lastIds.sh_activity_lastid >= 0) { //世瀚活动
        if (lastIds.sh_activity_lastid > this.data.actId) {
          this.setData({
            actIdflag: true
          })
        } else {
          this.setData({
            actIdflag: false
          })
        }
      } else {
        this.setData({
          actIdflag: false
        })
      }
      if (lastIds.sh_customer_salesrule_lastid >= 0) { //优惠券消息
        if (lastIds.sh_customer_salesrule_lastid > this.data.salId) {
          this.setData({
            salIdflag: true
          })
        } else {
          this.setData({
            salIdflag: false
          })
        }
      } else {
        this.setData({
          salIdflag: false
        })
      }
      if (lastIds.sh_partner_inform_lastid >= 0) { //合伙人消息
        if (lastIds.sh_partner_inform_lastid > this.data.infId) {
          this.setData({
            infIdflag: true
          })
        } else {
          this.setData({
            infIdflag: false
          })
        }
      } else {
        this.setData({
          infIdflag: false
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})